package com.ytkj.digitalConstruction.service.constructionprocess;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.ytkj.digitalConstruction.dao.constructionprocess.ConstructionReviewMapper;
import com.ytkj.digitalConstruction.entity.constructionplan.ConstructionPlan;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionBase;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionReview;
import com.ytkj.digitalConstruction.entity.user.User;
import com.ytkj.digitalConstruction.enums.construction.ConstructionResultEnum;
import com.ytkj.digitalConstruction.enums.construction.SpecialProcessEnum;
import com.ytkj.digitalConstruction.exception.CustomException;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.WholeFlowInfo;

/**
 * 复查相关业务
 * 
 * @author 宁~
 *
 */
@Service
@Transactional
public class ConstructionReviewService {

	@Autowired
	private ConstructionBaseService constructionBaseService;
	
	@Autowired
	private ConstructionReviewMapper constructionReviewMapper;
	
	@Autowired
	private ConstructionPlanService constructionPlanService;
	

	/**
	 * @Description: 监理确认
	 * @Title: setReview 
	 * @param @param constructionReview
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public WholeFlowInfo setReview(Integer flowId, User user){

		constructionBaseService.checkNowProcess(flowId, 11);

		ConstructionPlan plan = constructionPlanService.getConstructionPlan(flowId);
		/**
		 * (因需求变更此条废弃)取消施工或者指挥部未审批便手动结束此时直接结束，不走复查的安检确认
		 * 已变为取消施工不走复查的安检确认
		 */
		// boolean isFinish = plan.getIsconstruct() == 0 || (!base.getProcess().contains("9") && base.getStatus() == 2);
		boolean isFinish = plan.getIsconstruct() == 0;
				
		ConstructionReview constructionReview = new ConstructionReview();
		patchReviewByAdd(constructionReview);
		constructionReview.setFlowId(flowId);
		
		if(isFinish) {
			constructionReview.setStatus(1);
		}else {
			constructionReview.setStatus(0);
		}
		patchReview(flowId,constructionReview, user);
		constructionReviewMapper.insertSelective(constructionReview);
		ConstructionUtilService.isAddSuccess(constructionReview.getReviewId());
		/**
		 * (因需求变更此条废弃)取消施工或者指挥部未审批便手动结束此时直接结束，不走复查的安检确认
		 * 已变为取消施工不走复查的安检确认
		 */
		if(isFinish) {
			return constructionBaseService.processNextResult(flowId, user, 12, SpecialProcessEnum.CONFIRM_CANCEL_CONSTRUCTION);
		}
		return constructionBaseService.processNextResult(constructionReview.getFlowId(), user, 10);
	}
	
	/**
	 * @Description: 复查时安检确认
	 * @Title: confirmReview 
	 * @param @param flowId
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public WholeFlowInfo confirmReview(Integer flowId, User user){
		ConstructionBase base = constructionBaseService.checkNowProcess(flowId, 12);
		if(base.getFlowDate() != null) {
			throw new CustomException(ConstructionResultEnum.END_OR_CANCEL);
		}
		boolean isExceptionEnd = base.getStatus() == 2;
		
		ConstructionReview constructionReview = new ConstructionReview();
		/**
		 * 如果是手动结束则创建复查信息
		 */
		constructionReview.setFlowId(flowId);
		constructionReview.setStatus(1);
		patchReview(flowId,constructionReview, user);
		if(isExceptionEnd) {
			patchReviewByAdd(constructionReview);
			constructionReviewMapper.insertSelective(constructionReview);
			ConstructionUtilService.isAddSuccess(constructionReview.getReviewId());
		}else {
			ConstructionUtilService.isUpdateSuccess(constructionReviewMapper.updateByFlowIdSelective(constructionReview));
		}
		return constructionBaseService.processNextResult(constructionReview.getFlowId(), user, 11);
	}
	
	/**
	 * @Description: 补全参数
	 * @Title: patchReview 
	 * @param @param review
	 * @param @param user 
	 * @return void
	 * @throws
	 */
	public void patchReview(Integer flowId,ConstructionReview review, User user) {
		ConstructionReview existReview = constructionReviewMapper.selectByFlowId(flowId);
		if(existReview != null) {
			review.setOperator(existReview.getOperator());
		}
		
		if(StringUtils.isEmpty(review.getOperator())) {
			review.setOperator(String.valueOf(user.getUserId()));
		}else {
			review.setOperator(ConstructionUtilService.synthesisString(review.getOperator(),",",String.valueOf(user.getUserId())));
		}
		
		review.setUpdateBy(user.getUserId());
		review.setUpdateBy(user.getUserId());
		review.setUpdateDate(new Date());
	}
	
	/**
	 * @Description: 新增时所需补全的参数
	 * @Title: patchReviewByAdd 
	 * @param @param constructionReview    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	public void patchReviewByAdd(ConstructionReview constructionReview) {
		constructionReview.setDeviceCount(1);
		constructionReview.setFodManager(1);
		constructionReview.setPersonnelCount(1);
		constructionReview.setReviewId(null);
	}
}
